package com.esun.rabbit2.viewer.mainact.optionact;

import java.util.Random;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.esun.config.Config;
import com.esun.ui.R;
import com.esun.ui.Splash;

/**
 * 自定义Service,在其中启动Notification
 */
public class NotificationReceiver extends BroadcastReceiver {

	private ValuesManager valuesManager;

	@Override
	public void onReceive(Context context, Intent intent) {

		valuesManager = ValuesManager.getInstance(context);
		valuesManager.readValue();
		String action = intent.getAction();

		if (action.equals(Config.ALARMACTION)) {
			if (valuesManager.isState_masterPushSwitch() && valuesManager.isState_timePushSwitch()) {

				String strMessage = "您是不是该去玩玩";
				boolean isMutil = false;

				// 双色球
				if (valuesManager.isState_SSQ_timePushSwitch()) {

					strMessage = strMessage + "双色球";
					isMutil = true;

				}

				if (valuesManager.isState_DLT_timePushSwitch()) {

					if (isMutil) {
						strMessage = strMessage + "/";
					}
					strMessage = strMessage + "大乐透";
					isMutil = true;

				}

				if (valuesManager.isState_FC3D_timePushSwitch()) {

					if (isMutil) {
						strMessage = strMessage + "/";
					}
					strMessage = strMessage + "福彩3D";

				}

				strMessage = strMessage + " 试试手气";
				setNewNotification(context, "500WAN彩票", strMessage, "500WAN彩票:" + strMessage);

			}
		}

	}

	/**
	 * @Title: setNewNotification
	 * @Description: 新建一个推送实例
	 * @param: @param context
	 * @param: @param title
	 * @param: @param text
	 * @param: @param msg
	 * @return: void 返回类型
	 * @throws
	 */
	private void setNewNotification(Context context, String title, String text, String msg) {
		NotificationManager notiManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		notiManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		Notification notification = new Notification(R.drawable.logo500wan_48x48, msg, System.currentTimeMillis());
		// 设置提示框的状态
		notification.flags = Notification.FLAG_AUTO_CANCEL;
		Intent intent = new Intent(context, Splash.class);
		intent.setAction(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_LAUNCHER);
		// intent.setComponent(new ComponentName("com.esun.ui", "com.esun.ui.Splash"));
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		notification.setLatestEventInfo(context, title, text, contentIntent);
		notiManager.notify(new Random(System.currentTimeMillis()).nextInt(), notification);

	}

}